<?php


namespace app\admin\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;
use think\Validate;

class Navigation extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform",$platform);
        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }


    //导航列表
    public function index(){
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $thisWechat = Session::get("thisWechat");
        $where['uniacid'] = $thisWechat['id'];
        if(!empty($_GET['type'])) {
            try{
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_navigation")->where($where)->limit($start,$limit)->select();

                foreach ($list as $k=>$v){
                    $v['iconPath'] = empty($v['iconPath']) ? "/static/images/nopic-107.png" : $v['iconPath'];
                    $list[$k]['iconPath'] = "<img src='".$v['iconPath']."' style='max-height:50px;max-width:50px;'>";
                }

                $count = Db::name("admin_navigation")->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch('index');
    }

    //导航详情页面&添加视图
    public function detail(){
        $enable_status = Db::name("config_attribute")->where(['key'=>"enable_status"])->value("value");
        $enable_status = explode("|",$enable_status);
        foreach ($enable_status as $k=>$v){
            $enable_status[$k+1] = $v;
        }
        unset($enable_status[0]);
        $assign = [
            "enable_status" => $enable_status,
        ];
        $this->assign($assign);
        if(!empty($_POST['id'])){
            $detail = Db::name("admin_navigation")->where(['id'=>$_POST['id']])->find();
            $this->assign("detail",$detail);
            return view('update');
        }else{
            return view('create');
        }
    }

    //添加&修改操作
    public function doNavigation(){

        try{
            $common = new Common();
            $rule = [
                ["text", "require", "导航名称不能为空"],
                ["pagePath", "require", "导航地址不能为空"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }

            if(empty($value['id'])){
                $where['uniacid'] = $thisWechat['id'];
                $where['status'] = 1;
                $count = Db::name("admin_navigation")->where($where)->count("*");
                if($count>=5){
                    throw new Exception("只能配置最多5个tab");
                }
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $navid = Db::name("admin_navigation")->insertGetId($value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】添加新的导航栏目【".$value['text']."】,栏目id【".$navid."】" ;
            }else{
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                Db::name("admin_navigation")->update($value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】修改导航栏目【".$value['text']."】,栏目id【".$value['id']."】" ;
            }
            $log['controller'] = "Navigation";
            $log['methods'] = "doNavigation";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除导航操作
    public function delNavigation(){
        try{
            $id = $_POST['id'];
            $navList = Db::name("admin_navigation")->where(['id'=>['in',$id]])->select();
            $common = new Common();
            foreach ($navList as $k=>$v){
                //获取社区名称
                $log['remark'] = "管理员【" . Session::get("user_name") . "】删除导航【".$v['text']."】";
                $log['controller'] = "Navigation";
                $log['methods'] = "delNavigation";
                $common->addLog($log);
            }
            Db::name("admin_navigation")->where(['id'=>['in',$id]])->delete();
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}